package com.lrz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lrz.common.IdWorker;
import com.lrz.dao.ArticleMapper;
import com.lrz.dao.ColumnMapper;
import com.lrz.dao.TypeMapper;
import com.lrz.dao.UserMapper;
import com.lrz.pojo.Article;
import com.lrz.pojo.dto.ArticleDTO;
import com.lrz.service.ArticleService;
import com.lrz.service.LikeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private TypeMapper typeMapper;

    @Resource
    private ColumnMapper columnMapper;

    @Resource
    private LikeService likeService;

    @Resource
    private IdWorker idWorker;


    @Override
    public List<ArticleDTO> findAll() {
        List<Article> articles = articleMapper.selectList(null);
        ArrayList<ArticleDTO> articleDTOS = new ArrayList<>();
        for (Article article : articles) {
            ArticleDTO articleDTO = new ArticleDTO();
            articleDTO.setArticle(article);
            articleDTO.setUser(userMapper.selectById(article.getUserId()));
            articleDTO.setType(typeMapper.selectById(article.getTypeId()));
            articleDTO.setColumn(columnMapper.selectById(article.getColumnId()));
            articleDTOS.add(articleDTO);
        }
        return articleDTOS;
    }

    @Override
    public ArticleDTO findById(Long userId, Long id) {
        ArticleDTO articleDTO = new ArticleDTO();
        Article article = articleMapper.selectById(id);
        articleDTO.setUser(userMapper.selectById(userId));
        articleDTO.setType(typeMapper.selectById(article.getTypeId()));
        articleDTO.setColumn(columnMapper.selectById(article.getColumnId()));
        articleDTO.setArticle(article);
        articleDTO.setContent(article.getContent());
        articleDTO.setLike(likeService.isLike(String.valueOf(id), userId));
        return articleDTO;
    }

    @Override
    public List<Article> findByUserId(Long userId) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("userid", userId);
        return articleMapper.selectList(wrapper);
    }

    @Override
    public List<Article> findByState() {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("state", "1");
        return articleMapper.selectList(wrapper);
    }

    @Override
    public void add(Article article) {
        article.setId(idWorker.nextId());
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setState("0");
        article.setVisits(0);
        article.setThumbup(0);
        article.setComment(0);
        articleMapper.insert(article);
    }

    @Override
    public void update(Article article) {
        articleMapper.updateById(article);
    }

    @Override
    public void delete(Long id) {
        articleMapper.deleteById(id);
    }

    @Override
    public void addThumbup(Long id) {
        Article article = articleMapper.selectById(id);
        article.setThumbup(article.getThumbup() + 1);
        articleMapper.updateById(article);
    }

    @Override
    public void decThumbup(Long id) {
        Article article = articleMapper.selectById(id);
        article.setThumbup(article.getThumbup() - 1);
        articleMapper.updateById(article);
    }

}
